package com.wang.tool.delivery.base;

import com.wang.tool.delivery.DeliveryQueryService;
import com.wang.tool.delivery.cache.module.ModuleCacheDTO;
import com.wang.tool.delivery.cache.page.PageCacheDTO;
import com.wang.tool.delivery.cache.plan.PlanCacheDTO;
import com.wang.tool.delivery.cache.resource.ResourceCacheDTO;
import com.wang.tool.delivery.cache.route.RouteScopeCacheDTO;
import com.wang.tool.delivery.cache.scene.SceneCacheDTO;
import com.wang.tool.delivery.plan.CachePageInfo;
import com.wang.tool.delivery.RequestBox;
import com.wang.tool.common.Container;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 投放盒
 * 包装了一些常用取放置逻辑,仅为了复用一次请求中数据缓存
 *
 * @author shaopeng
 * @date 2022/8/8
 */
public class DeliveryBox extends RequestBox {


    private Set<String> loadedSubject = new HashSet<>();


    private DeliveryQueryService deliveryQueryService;

    private ExecutorService executorService;


    public void tryMarkLoadSubject(String subjectId){
        //加载成环
        if (!loadedSubject.add(subjectId)) {
            throw new IllegalArgumentException();
        }
    }


    public SceneCacheDTO getSceneFromCache(String sceneCode) {
        return getOrLoad("scene", sceneCode, () -> {
            return Container.build(deliveryQueryService.getSceneFromCache(sceneCode));
        });
    }



    public PageCacheDTO getPageFromCache(String pageId) {
        return getOrLoad("page", pageId, () -> {
            return Container.build(deliveryQueryService.getPageFromCache(pageId));
        });
    }


    public Map<String, ModuleCacheDTO> getModuleFromCache(List<String> moduleIds){
        return batchGet("module", moduleIds, new Function<Collection<String>, Map<String, Container<ModuleCacheDTO>>>() {
            @Override
            public Map<String, Container<ModuleCacheDTO>> apply(Collection<String> strings) {
                List<ModuleCacheDTO> moduleCacheDTOS = deliveryQueryService.getModuleFromCache(new ArrayList<>(strings));
                if (moduleCacheDTOS==null) {
                    return null;
                }
                return moduleCacheDTOS.stream().collect(Collectors.toMap(ModuleCacheDTO::getModuleId, Container::build));
            }
        });
    };

    public RouteScopeCacheDTO getRouteScopeFromCache(String routeId) {
        //
        return null;
    }


    public Map<String, ResourceCacheDTO> getResourceFromCache(Collection<String> resourceIds){
        //todo
        return null;
    };

    public Map<String,List<PlanCacheDTO>> getResourcePlanFromCache(Collection<String> resourceIds){
        //todo
        return null;
    };


    public Map<String, PlanCacheDTO> getPlanFromCache(Collection<String> planIds){;
        //todo
        return null;
    };


    public Map<String,CachePageInfo> getPageInfoFromCache(String session,String resourceId){
        //todo
        return null;
    }


    public void putPageInfoToCache(String session, String resourceId, Map<String, CachePageInfo> pageInfo){
        //todo
    }


    public DeliveryQueryService getDeliveryQueryService() {
        return deliveryQueryService;
    }

    public void setDeliveryQueryService(DeliveryQueryService deliveryQueryService) {
        this.deliveryQueryService = deliveryQueryService;
    }

    public ExecutorService getExecutorService() {
        return executorService;
    }

    public void setExecutorService(ExecutorService executorService) {
        this.executorService = executorService;
    }
}
